package com.xy.mainp.novel.fragment.home;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xy.debug.SystemConfig;
import com.xy.mainp.NovelHomeBinding;
import com.xy.mainp.R;
import com.xy.mainp.base.LazyFragment;
import com.xy.mainp.model.biqu.home.BiquHomeHotEntity;
import com.xy.mainp.model.biqu.home.classify.ClassfiyBookEntity;
import com.xy.mainp.model.biqu.home.classify.ClassfiyBookItemEntity;
import com.xy.mainp.model.biqu.home.novellist.NovelListEntity;
import com.xy.mainp.novel.fragment.home.ui.NovelHomeCanstract;
import com.xy.mainp.novel.fragment.home.ui.NovelHomePresenter;
import com.xy.mainp.novel.fragment.home.widget.ClassfiyBookItemView;
import com.xy.mainp.novel.fragment.home.widget.HotBookItemView;
import com.xy.view.AutoScrollViewPager;
import com.xy.view.StandardAdapter;

import java.util.List;


/**
 * Created by John on 2016/11/28.
 */

public class NovelHomeFragment extends LazyFragment implements NovelHomeCanstract.NovelHomeViewImpl {

    private String url;
    private NovelHomeBinding binding;
    private NovelHomePresenter presenter;

    public NovelHomeFragment() {

    }

    public NovelHomeFragment(String url) {
        this.url = url;
    }


    @Override
    protected void onIn() {
        super.onIn();
        presenter = new NovelHomePresenter(this);
    }

    @Override
    protected void onShow() {
        super.onShow();
        presenter.getData(url);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_novel_home, null, false);
        setContentView(binding.getRoot());
    }

    @Override
    public void setHotView(List<BiquHomeHotEntity> list) {
        binding.asVp.setDelay(2000);
        binding.asVp.setAdapter(new NovelHomeVpAdapter(list));
        binding.asVp.setPageMargin(SystemConfig.dp2px(getContext(),6));
        binding.asVp.startScroll();
    }

    @Override
    public void setClassfiyView(ClassfiyBookEntity entity) {
        binding.tvClassfiyTittle.setText(entity.getTittle());
        binding.tvClassfiyTittle.setVisibility(TextUtils.isEmpty(entity.getTittle()) ? View.GONE : View.VISIBLE);
        ClassFiyAdapter adapter = new ClassFiyAdapter();
        adapter.setData(entity.getItemEntities());
        binding.lvClassfiyBook.setAdapter(adapter);
    }

    public void setBookList(List<NovelListEntity> bookList) {
        binding.viewBookList.setData(bookList);
    }

    class ClassFiyAdapter extends StandardAdapter {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = new ClassfiyBookItemView(parent.getContext());
            }
            ClassfiyBookItemView view = (ClassfiyBookItemView) convertView;
            view.setData((ClassfiyBookItemEntity) getItem(position));
            return view;
        }
    }

    class NovelHomeVpAdapter extends AutoScrollViewPager.AutoScrollPagerAdapter{

        private List<BiquHomeHotEntity> list;

        public NovelHomeVpAdapter(List<BiquHomeHotEntity> list){
            this.list = list;
        }

        @Override
        public int getDataCount() {
            return list != null ? list.size() : 0;
        }

        @Override
        public View createView(Context context, int position) {
            HotBookItemView hotBookItemView = new HotBookItemView(context);
            hotBookItemView.setData(list.get(position));
            return hotBookItemView;
        }
    }
}
